// pages/integralShop/integralExchange/index.js
const {
  IntegralShopModel
} = require('../../../utils/integralshop-model.js');
const {
  MemberModel
} = require('../../../utils/member-model.js')
const memberModel = new MemberModel()
var integralShopModel = new IntegralShopModel();
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isLogin: false, //是否登录
    mallLayoutId: "", //商城布局id
    islayerShow: false, //是否显示弹出层
    refName: "", //券名称
    imageUrl: "https://oss.kiscloud.net/wechat/images/icon_no_photo_2.png", //图片
    price: 0, //价格
    buyLimit: "", //兑换限制
    memberLevel: "", //等级限制
    expirationLimit: "", //有效期
    accumAmount: "", //最低消费
    note: "", //温馨提示
    disabled: false, //是否兑换
    authShow: false,
    discountLimit: 0, //最大折扣金额
    promotionAmount: 0, //赠送金额or折扣率
    maxAmount: "", //类型 0 优惠券 1兑换券
    // unavailableReason: '', //不可用原因
    needPointAmount: 0, //需要的积分
    limitType: null, //限制规则 -1无限制 0每天 1每周
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      mallLayoutId: options.mallLayoutId
    })
  },

  /**
   * 登录过的积分商品详情
   */
  getItemAppletGet: function () {
    this.setData({
      isLogin: app.globalData.isLogin
    });
    let param = {
      mallLayoutId: this.data.mallLayoutId,
      merchantId: wx.getStorageSync('merchantId'), //商户id
    }
    let request = null
    if (app.globalData.isLogin) {
      request = integralShopModel.itemAppletGet(param)
    } else {
      request = integralShopModel.tokenAppletGet(param)
    }
    request.then(data => {
      this.integralShopInfo(data);
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 积分商品详情
   */
  integralShopInfo(data) {
    var that = this;
    var validityTime = "";
    var validityTitle = "";
    if (data.expirationType == 0) {
      validityTime = "领取后" + data.expirationLimit + "内有效";
      validityTitle = "有效期限：";
    } else if (data.expirationType == 1) {
      validityTime = data.expirationLimit;
      validityTitle = "有效期至：";
    }
    that.setData({
      refName: data.refName || "", //券名称
      imageUrl: data.imageUrl || "https://oss.kiscloud.net/wechat/images/icon_no_photo_2.png", //图片
      price: data.price || 0, //价格
      buyLimit: data.buyLimit || 0, //兑换限制
      memberLevel: data.memberLevel == -1 ? "无等级限制" : "V" + data.memberLevel + "及以上可兑换" || "", //等级限制
      accumAmount: data.accumAmount == 0 ? "无限制" : data.accumAmount > 0 ? "最低消费" + data.accumAmount + "元" : "" || "", //使用限制
      note: data.note || '', //温馨提示
      expirationType: data.expirationType || "", //有效期类型 0-相对时间，1-指定时间
      expirationLimit: validityTime || "", //有效期
      unavailableReason: data.unavailableReason, //不可用原因
      validityTitle: validityTitle, //标题
      discountLimit: "最高折扣金额" + data.discountLimit + "元(超过则一律按此金额扣减)", //最大折扣金额
      maxAmount: "最高消费" + data.maxAmount + "元", //最大消费金额
      refType: data.refType, //类型 0 优惠券 1兑换券
      needPointAmount: data.needPointAmount || 0, //需要的积分
      limitType: data.limitType, //限制规则 -1无限制 0每天 1每周
    })
  },


  /**
   *积分兑换弹出层
   */
  setExchange: function () {
    if (app.globalData.isLogin) {
      this.setData({
        islayerShow: !this.data.islayerShow
      });
    } else {
      this.setData({
        authShow: true
      })
    }
  },

  formSubmit(e) {
    memberModel.userMessage({
      formIds: [e.detail.formId],
      merchantId: wx.getStorageSync('merchantId')
    }).then(() => {
      this.setItemAppletExchange()
    }).catch(() => {
      this.setItemAppletExchange()
    })
  },

  /**
   *积分兑换
   */
  setItemAppletExchange: function () {
    this.setData({
      disabled: true
    }, function () {
      var param = {};
      param.merchantId = wx.getStorageSync('merchantId'); //商户id
      param.mallLayoutId = this.data.mallLayoutId; //商城布局id
      integralShopModel.itemAppletExchange(param).then(data => {
        this.setData({
          disabled: false
        }, function () {
          wx.redirectTo({
            url: '/pages/integralShop/integralMessage/index?price=' + this.data.price
          })
        });
      }).catch(err => {
        this.setData({
          disabled: false
        })
        wx.showToast({
          title: `${err.error}`,
          icon: 'none',
          duration: 2000
        })
      })
    });
  },
  onShow: function () {
    this.setData({
      isLogin: app.globalData.isLogin
    }, function () {
      this.getItemAppletGet();
    })
  }
})